package com.tdam2011.grupo05.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.tdam2011.grupo05.entities.account.Account;
import com.tdam2011.grupo05.entities.contact.ContactWeb;
import com.tdam2011.grupo05.entities.logs.LogImp;
import com.tdam2011.grupo05.entities.message.MessageWeb;
import com.tdam2011.grupo05.entities.thread.ThreadWeb;

public class DatabaseHelper extends SQLiteOpenHelper {

	public static final String dbName = "tdam";

	public DatabaseHelper(Context context) {
		super(context, dbName, null, 1);

	}

	@Override
	public void onCreate(SQLiteDatabase db) {

		db.execSQL("CREATE TABLE " + Account.TABLE_NAME + " ( "
				+ Account.COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
				+ Account.COL_LAST_SYNC + " BIGINT, " + Account.COL_USERNAME
				+ " TEXT, " + Account.COL_PASSWORD + " TEXT)");

		db.execSQL("CREATE TABLE " + MessageWeb.TABLE_NAME + " ( "
				+ MessageWeb.COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
				+ MessageWeb.COL_ID_THREAD + " INTEGER, " + MessageWeb.COL_TEXT
				+ " TEXT, " + MessageWeb.COL_TIMESTAMP + " BIGINT, "
				+ MessageWeb.COL_RESULT + " TEXT, " + MessageWeb.COL_TYPE
				+ " TEXT, " + MessageWeb.COL_STATE + " TEXT)");

		db.execSQL("CREATE TABLE " + ThreadWeb.TABLE_NAME + " ( "
				+ ThreadWeb.COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
				+ ThreadWeb.COL_TIMESTAMP + " BIGINT, " + ThreadWeb.COL_STATE
				+ " TEXT, " + ThreadWeb.COL_NAME + " TEXT, "
				+ ThreadWeb.COL_ID_ACCOUNT + " INTEGER, "
				+ ThreadWeb.COL_ID_USER + " INTEGER)");

		db.execSQL("CREATE TABLE " + ContactWeb.TABLE_NAME + " ("
				+ ContactWeb.COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
				+ ContactWeb.COL_ID_CONTACTO + " INTEGER, "
				+ ContactWeb.COL_ID_PHONE + " INTEGER, " + ContactWeb.COL_NAME
				+ " TEXT )");

		db.execSQL("CREATE TABLE " + LogImp.TABLE_NAME + " ("
				+ ContactWeb.COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
				+ LogImp.COL_CONTACT_NAME + " TEXT, " + LogImp.COL_DATE
				+ " BIGINT, " + LogImp.COL_DETAIL + " TEXT, "
				+ LogImp.COL_DATA + " TEXT, " + LogImp.COL_TYPE + " INTEGER )");

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

		db.execSQL("DROP TABLE IF EXISTS " + dbName);
		db.execSQL("DROP TABLE IF EXISTS " + Account.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + ContactWeb.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + ThreadWeb.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + MessageWeb.TABLE_NAME);
		onCreate(db);
	}

}
